﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using ShareContent;

namespace AncientEmpires_025_132
{
    public class VisibleGameEntity : GameEntity
    {
        private List<Sprite2D> _listSprites;
        public List<Sprite2D> ListSprites
        {
            get { return _listSprites; }
            set { _listSprites = value; }
        }
        private int _iSprite = 0;
        public int ISprite
        {
            get { return _iSprite; }
            set { _iSprite = value; }
        }
        private bool _isHide;
        public bool IsHide
        {
            get { return _isHide; }
            set { _isHide = value; }
        }

        private bool _isHover;
        public bool IsHover
        {
            get { return _isHover; }
            set { _isHover = value; }
        }

        private Vector2 _positionScale;
        public Vector2 PositionScale
        {
            get { return _positionScale; }
            set { _positionScale = value; }
        }
        private Vector2 _sizeTexture;
        public Vector2 SizeTexture
        {
            get { return _sizeTexture; }
            set { _sizeTexture = value; }
        }

        private Vector2 _sizeScale;
        public Vector2 SizeScale
        {
            get { return _sizeScale; }
            set { _sizeScale = value; }
        }
        private ButtonState _preMouseState;
        public ButtonState PreMouseState
        {
            get { return _preMouseState; }
            set { _preMouseState = value; }
        }

        protected Sound mySound;
        public Sound MySound
        {
            get { return mySound; }
            set { mySound = value; }
        }
        private Vector2 _viewPort;
        public Vector2 ViewPort
        {
            get { return _viewPort; }
            set { _viewPort = value; }
        }

        private List<PixelColor> listChangeColor;
        public List<PixelColor> ListChangeColor
        {
            get { return listChangeColor; }
            set { listChangeColor = value; }
        }


        public virtual void SetViewPort(int cellWidth, int cellHeigth)
        {
            ViewPort = new Vector2(0, cellHeigth - this.ListSprites[0].Textures[0].Height);
        }
        public virtual void UpdateMouseState()
        {
        }

        public virtual bool CheckClickSprite(Vector2 ms)
        {
            if (ms.X >= this.ListSprites[ISprite].Position.X
               && ms.X <= this.ListSprites[ISprite].Position.X + this.ListSprites[ISprite].Size.X
               && ms.Y >= this.ListSprites[ISprite].Position.Y
               && ms.Y <= this.ListSprites[ISprite].Position.Y + this.ListSprites[ISprite].Size.Y)
            {
                return true;
            }
            return false;
        }
        public virtual bool CheckHoverSpirte(Vector2 ms)
        {
            if (ms.X >= this.ListSprites[ISprite].Position.X
               && ms.X <= this.ListSprites[ISprite].Position.X + this.ListSprites[ISprite].Size.X
               && ms.Y >= this.ListSprites[ISprite].Position.Y
               && ms.Y <= this.ListSprites[ISprite].Position.Y + this.ListSprites[ISprite].Size.Y)
            {
                return true;
            }
            return false;
        }
        
        //Change character after nation color
        public virtual void SetColorAfterNation(List<PixelColor> listBeChangeColor, NationName nationName)
        {
            List<Color> listColor = new List<Color>();
            for (int i = 0; i < listBeChangeColor.Count; i++)
            {
                Color temp = new Color(listBeChangeColor[i].R, listBeChangeColor[i].G, listBeChangeColor[i].B);

                listColor.Add(temp);
            }
            for (int i = 0; i < ListSprites.Count; i++)
            {
                for (int j = 0; j < ListSprites[i].NTextures; j++)
                {
                    Texture2D changeTexture = ListSprites[i].Textures[j];
                    changeTexture = SetTextureColorAfterNation(listColor, changeTexture, nationName);
                    ListSprites[i].Textures[j] = changeTexture;
                }
            }
        }

        public virtual void SetColorAfterNation(NationName nationName)
        {
            List<PixelColor> newListColor = new List<PixelColor>();
            List<Color> listColor = new List<Color>();
            for (int i = 0; i < listChangeColor.Count; i++)
            {
                Color temp = new Color(listChangeColor[i].R, listChangeColor[i].G, listChangeColor[i].B);

                listColor.Add(temp);
            }
            for (int i = 0; i < ListSprites.Count; i++)
            {
                for (int j = 0; j < ListSprites[i].NTextures; j++)
                {
                    Texture2D changeTexture = ListSprites[i].Textures[j];
                    changeTexture = SetTextureColorAfterNation(listColor, changeTexture, nationName);
                    ListSprites[i].Textures[j] = changeTexture;
                }
            }
            this.listChangeColor = newListColor;
        }

        //Change color texture after nation color
        public virtual Texture2D SetTextureColorAfterNation(List<Color> listBeChangeColor, Texture2D changeTexture, NationName nationName)
        {
            Color[] data;
            data = new Color[changeTexture.Width * changeTexture.Height];
            changeTexture.GetData(data);
            for (int i = 0; i < data.Length; i++)
            {
                for (int j = 0; j < listBeChangeColor.Count; j++)
                {
                    if (data[i] == listBeChangeColor[j])
                    {
                        int b = 0, r = 0, g = 0;
                        switch (nationName)
                        {
                            case NationName.Blue:
                                b = (int)MathHelper.Max(MathHelper.Max(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                r = (int)MathHelper.Min(MathHelper.Min(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                if (listBeChangeColor[j].R < b && listBeChangeColor[j].R > r) g = listBeChangeColor[j].R;
                                else if (listBeChangeColor[j].B < b && listBeChangeColor[j].B > r) g = listBeChangeColor[j].B;
                                else if (listBeChangeColor[j].G < b && listBeChangeColor[j].G > r) g = listBeChangeColor[j].G;
                                break;
                            case NationName.Green:
                                g = (int)MathHelper.Max(MathHelper.Max(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                b = (int)MathHelper.Min(MathHelper.Min(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                if (listBeChangeColor[j].R < g && listBeChangeColor[j].R > b) r = listBeChangeColor[j].R;
                                else if (listBeChangeColor[j].B < g && listBeChangeColor[j].B > b) r = listBeChangeColor[j].B;
                                else if (listBeChangeColor[j].G < g && listBeChangeColor[j].G > b) r = listBeChangeColor[j].G;
                                break;
                            case NationName.Red:
                                r = (int)MathHelper.Max(MathHelper.Max(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                b = (int)MathHelper.Min(MathHelper.Min(listBeChangeColor[j].R, listBeChangeColor[j].B), listBeChangeColor[j].G);
                                if (listBeChangeColor[j].R < r && listBeChangeColor[j].R > b) g = listBeChangeColor[j].R;
                                else if (listBeChangeColor[j].B < r && listBeChangeColor[j].B > b) g = listBeChangeColor[j].B;
                                else if (listBeChangeColor[j].G < r && listBeChangeColor[j].G > b) g = listBeChangeColor[j].G;
                                break;
                            case NationName.Black:
                                r = (int)Math.Sqrt(listBeChangeColor[j].R);
                                b = (int)Math.Sqrt(listBeChangeColor[j].B);
                                g = (int)Math.Sqrt(listBeChangeColor[j].G);
                                break;
                        }
                        data[i] = new Color(r, b, g);
                        break;
                    }
                }
            }
            changeTexture.SetData(data);
            return changeTexture;
        }

        public override void Update(GameTime gametime)
        {
            for (int i = 0; i < ListSprites.Count; i++)
            {
                ListSprites[i].Position = -ListSprites[i].TopLeft;
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            int i = ISprite;
            Rectangle rect = new Rectangle();
            if (ListSprites[i].Size.X == 0 && ListSprites[i].Size.Y == 0)
            {
                ListSprites[i].Size = new Vector2(ListSprites[i].Textures[ListSprites[i].ITexture].Width, ListSprites[i].Textures[ListSprites[i].ITexture].Height);
            }
            rect = new Rectangle((int)ListSprites[i].Position.X, (int)ListSprites[i].Position.Y, (int)ListSprites[i].Size.X, (int)ListSprites[i].Size.Y);
            spriteBatch.Draw(ListSprites[i].Textures[ListSprites[i].ITexture], rect, Color.White);
                
            
        }
        
    }
}
